package com.caiheng.api.service.platfom.impl;

import com.caiheng.api.dao.platform.CouponsMapper;
import com.caiheng.api.dao.wxuser.UserCouponsMapper;
import com.caiheng.api.dao.wxuser.UserIntegralRecordMapper;
import com.caiheng.api.dao.wxuser.UserMapper;
import com.caiheng.api.entity.platform.Coupons;
import com.caiheng.api.entity.wxuser.User;
import com.caiheng.api.entity.wxuser.UserCoupons;
import com.caiheng.api.entity.wxuser.UserIntegralRecord;
import com.caiheng.api.exception.TransactionalException;
import com.caiheng.api.service.base.impl.BaseServiceImpl;
import com.caiheng.api.service.platfom.CouponsService;
import com.caiheng.api.util.DateUtil;
import com.caiheng.api.util.MathUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class CouponsServiceImpl extends BaseServiceImpl<CouponsMapper, Coupons> implements CouponsService {

    @Resource
    private CouponsMapper couponsMapper;
    @Resource
    private UserCouponsMapper userCouponsMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserIntegralRecordMapper userIntegralRecordMapper;


    @Override
    @Transactional
    public int addCoupon(User user, Coupons coupons) {
        int ret = 0;
        try{
            double price = coupons.getSinglePrice().doubleValue();

//            int ydCount = user.getIntegral();
//            ydCount = MathUtil.getDoubleUp(ydCount - price);
//            user.setIntegral(ydCount);
//            ret = userMapper.updateById(user);
//            if(ret <= 0){
//                throw new TransactionalException("userMapper updateById error");
//            }

            //购买优惠券使用
            UserIntegralRecord integralRecord = new UserIntegralRecord();
            integralRecord.setUserId(user.getId());
            integralRecord.setNickName(user.getNickName());
            integralRecord.setCreateTime(new Date());
            //积分的获取类型   1 - 购买商品、商家服务获取  2 -- 缴纳物业费使用  3 - 单独使用积分（支付物业费）  4 - 新用户注册赠送  5 - 购买优惠券
            integralRecord.setGetType(5);
            //增加积分  还是 减少积分  0 - 减少  1 - 加
            integralRecord.setInOrOut(0);
            integralRecord.setState(1);
            integralRecord.setIntegral(price);
            integralRecord.setRemark("购买" + coupons.getCouponName() +"使用。");
            ret = userIntegralRecordMapper.insert(integralRecord);
            if(ret <= 0){
                throw new TransactionalException("userIntegralRecordMapper insert error");
            }


            UserCoupons userCoupons = new UserCoupons();
            userCoupons.setUserId(user.getId());
            userCoupons.setState(0);
            userCoupons.setCreateTime(new Date());
            userCoupons.setCouponId(coupons.getId());
            userCoupons.setCouponName(coupons.getCouponName());
            userCoupons.setDikouPrice(coupons.getDikouPrice());
            userCoupons.setPrice(coupons.getSinglePrice());
            if(coupons.getExpireDays() > 0){
                userCoupons.setEndTime(DateUtil.getDateByDays(coupons.getExpireDays(),0));
            }
            ret = userCouponsMapper.insert(userCoupons);
            if(ret <= 0){
                throw new TransactionalException("userCouponsMapper insert error");
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return ret;
    }
}
